﻿@using System.Web.UI.WebControls.WebParts
@using System.Xml
@using System.Xml.Linq
@{
    var feed = Model.Feed as XElement;
    var content = XNamespace.Get("http://purl.org/rss/1.0/modules/content/");
    var dc = XNamespace.Get("http://purl.org/dc/elements/1.1/");
    var domainLink = (Model.Feed.Element("channel").Element("link") != null) ? Model.Feed.Element("channel").Element("link").Value : ""; 
}
@if (feed != null) {
    <ul>
        @foreach (var item in feed
            .Element("channel")
            .Elements("item")
            .Take((int) Model.ItemsToDisplay)) {
            <li>
                @if (Model.ContentPart.ShowTitleInHeader) {
                    <h3>@if (Model.ContentPart.LinkTitle) {
                            <a href="@item.Element("link").Value">@item.Element("title").Value</a>
                        }
                        else {
                            @item.Element("title").Value
                        }</h3>
                }
                @if (Model.ContentPart.ShowAuthorInHeader)
                {
                    <h4>
                        @{
                            var xElemnentDc = item.Element(dc + "creator");
                        }
                        @if (xElemnentDc != null)
                        {
                            <span>By</span> @Html.Raw(@xElemnentDc.Value)
                        }
                </h4>
                }
                @if (Model.ContentPart.ShowDomainInHeader)
                {
                    <div>@if (Model.ContentPart.LinkDomain)
                        {
                            <a href="@domainLink">@domainLink</a>
                        }
                        else {
                            @domainLink
                        }</div>
                }
                @{
                    var xElement = item.Element(content + "encoded");
                }
                @if (xElement != null) {
                    @Html.Raw(@xElement.Value)
                }
                else {
                    @Html.Raw(item.Element("description").Value)
                }
                @if (Model.ContentPart.ShowTitleInFooter)
                {
                    <h3>
                        @if (Model.ContentPart.LinkTitle)
                        {
                            <a href="@item.Element("link").Value">@item.Element("title").Value</a>
                        }
                        else
                        {
                            @item.Element("title").Value
                        }
                </h3>
                }
                @if (Model.ContentPart.ShowAuthorInFooter) {
                    <h4>
                        @{
                            var xElemnentDc = item.Element(dc + "creator");
                        }
                        @if (xElemnentDc != null) {
                            <span>By</span> @Html.Raw(@xElemnentDc.Value)
                        }
                    </h4>
                }
                @if (Model.ContentPart.ShowDomainInFooter)
                {
                    <div>
                        @if (Model.ContentPart.LinkDomain) {
                            <a href="@domainLink">@domainLink</a>
                        }
                        else {
                            @domainLink
                        }
                    </div>
                }
            </li>
        }
    </ul>
}